package com.flashcards.auth;

import com.flashcards.FlashCards;
import weibo4andriod.Weibo;

public class UserInfo {
    private String userId;
    private String userKey;
    private String userSecret;

    public UserInfo(String userId, String userKey, String userSecret) {
        this.userId = userId;
        this.userKey = userKey;
        this.userSecret = userSecret;
    }

    @Override
    public String toString() {
        return serializeAsString();
    }

    public String serializeAsString() {
        return userId + "," + userKey + "," + userSecret;
    }

    public static UserInfo deserialize(String serializedString) {
        if (serializedString == null || serializedString.length() == 0) {
            return new NullUserInfo();
        }
        String[] split = serializedString.split(",");
        return new UserInfo(split[0], split[1], split[2]);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserInfo userInfo = (UserInfo) o;

        if (userId != null ? !userId.equals(userInfo.userId) : userInfo.userId != null) return false;
        if (userKey != null ? !userKey.equals(userInfo.userKey) : userInfo.userKey != null) return false;
        if (userSecret != null ? !userSecret.equals(userInfo.userSecret) : userInfo.userSecret != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = userId != null ? userId.hashCode() : 0;
        result = 31 * result + (userKey != null ? userKey.hashCode() : 0);
        result = 31 * result + (userSecret != null ? userSecret.hashCode() : 0);
        return result;
    }

    public Weibo initialize(Weibo weibo) {
        weibo.setToken(this.userKey, this.userSecret);
        return weibo;
    }

    public Class targetActivity() {
        return FlashCards.class;
    }
}
